<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>

    <script type="text/javascript" src="../static/js/layui/layui.js"></script>
    <link rel="stylesheet" type="text/css" href="../static/js/layui/css/layui.css"/>
    <script type="text/javascript" src="../static/js/jquery.min.js"></script>
    <link rel="stylesheet" href="../static/css/index.css"/>
    <script type="text/javascript" src="../static/js/index.js"></script>

    <link rel="stylesheet" type="text/css" href="../static/js/layui/css/formSelects-v4.css"/>
</head>
<body class="body-common">
<div class="page-container">
    <form class="layui-form layui-form-pane" >

        <blockquote class="layui-elem-quote layui-text">
            单表生成代码
        </blockquote>
        <input type="hidden" name="primaryKeys" id="primaryKeys">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择表</label>
            <div class="layui-input-block">
                <select name="dataName" id="dataName" xm-select="dataName" xm-select-radio="" xm-select-search="" lay-verify="required">
                    <option >test</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="button" class="layui-btn" onclick="watchs()"   value="生成预览" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">表注释</label>
            <div class="layui-input-block">
                <input type="text" name="tableRemarks"  id="tableRemarks" lay-verify="required" autocomplete="off"
                       placeholder="" class="layui-input"  >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">实体名称</label>
            <div class="layui-input-block">
                <input type="text" name="tableName"  id="tableName" lay-verify="required" autocomplete="off"
                       placeholder="" class="layui-input"  >
            </div>
        </div>
        <table id="table-data" lay-filter="table-data"></table>
        <div class="layui-form-item" style="text-align: center">
            <button class="layui-btn" lay-submit="" lay-filter="save-data" >确认生成</button>
        </div>
    </form>
</div>
<!--
<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>-->
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    $(function () {
        $.post("../data/global/getTablesName",function (data) {
            if(data.code==0){
                var d=data.data;
                var text="";
                for (var i=0;i<d.length;i++){
                    text+="<option value=\""+d[i]+"\">"+d[i]+"</option>";
                }
                $("#dataName").html(text);
            }else{
                layer.alert("查询失败->"+data.message)
            }
        })
    })

    // var data={"code":"0","message":"成功","time":null,"data":{"primaryKey":[{"jdbcName":"id","beanName":"id","jdbcType":"4","beanType":"java.lang.Integer","nullable":false,"length":"10","identity":true,"remarks":"主键","defaultValue":null,"json":false,"key":true,"beanName_":"Id","jdbcType_":"INTEGER","beanType_":"Integer"}],"cons":null,"tableRemarks":"单表代码生成","allColumns":[{"jdbcName":"id","beanName":"id","jdbcType":"4","beanType":"java.lang.Integer","nullable":false,"length":"10","identity":true,"remarks":"主键","defaultValue":null,"json":false,"key":true,"beanName_":"Id","jdbcType_":"INTEGER","beanType_":"Integer"},{"jdbcName":"name","beanName":"name","jdbcType":"12","beanType":"java.lang.String","nullable":true,"length":"50","identity":false,"remarks":"名称","defaultValue":null,"json":false,"key":false,"beanName_":"Name","jdbcType_":"VARCHAR","beanType_":"String"},{"jdbcName":"age","beanName":"age","jdbcType":"4","beanType":"java.lang.Integer","nullable":true,"length":"10","identity":false,"remarks":"年龄","defaultValue":null,"json":false,"key":false,"beanName_":"Age","jdbcType_":"INTEGER","beanType_":"Integer"},{"jdbcName":"status","beanName":"status","jdbcType":"4","beanType":"java.lang.Integer","nullable":true,"length":"10","identity":false,"remarks":"{\"name\":\"状态\",\"1\":\"启用\",\"0\":\"禁用\"}","defaultValue":null,"json":false,"key":false,"beanName_":"Status","jdbcType_":"INTEGER","beanType_":"Integer"},{"jdbcName":"birthday","beanName":"birthday","jdbcType":"91","beanType":"java.util.Date","nullable":true,"length":"10","identity":false,"remarks":"生日","defaultValue":null,"json":false,"key":false,"beanName_":"Birthday","jdbcType_":"DATE","beanType_":"Date"},{"jdbcName":"remarks","beanName":"remarks","jdbcType":"-1","beanType":"java.lang.String","nullable":true,"length":"65535","identity":false,"remarks":"备注","defaultValue":null,"json":false,"key":false,"beanName_":"Remarks","jdbcType_":"LONGVARCHAR","beanType_":"String"},{"jdbcName":"create_time","beanName":"createTime","jdbcType":"93","beanType":"java.util.Date","nullable":true,"length":"19","identity":false,"remarks":"创建时间","defaultValue":"CURRENT_TIMESTAMP","json":false,"key":false,"beanName_":"CreateTime","jdbcType_":"TIMESTAMP","beanType_":"Date"}],"parentPack":"com.zengtengpeng.test","mobelName":"test","tableName":"TestSimpleCode","tableValue":"testSimpleCode","dataName":"test_simple_code"}}
    function watchs(){
        if(!formSelects.value('dataName', 'valStr')){
            layer.alert("请选择需要生成的表");
            return;
        }
        $.post("../data/simple/saxTable",{'dataName':formSelects.value('dataName', 'valStr')},function (data) {
            if(data.code==0){
                var d=data.data;
               $("#tableRemarks").val(d.tableRemarks);
               $("#tableName").val(d.tableName);
               $("#primaryKeys").val(JSON.stringify(d.primaryKey));

                table.render({
                    elem: '#table-data'
                    , cols: [[ //表头
                        {field: 'checked', title: 'checked', type: "checkbox","LAY_CHECKED":"true"}
                        ,{field: 'jdbcName', title: '数据库名称'}
                        ,{field: 'beanName', title: '实体名称(可修改)',edit: 'text'}
                        ,{field: 'jdbcType', title: '数据库表类型',hide:true}
                        ,{field: 'jdbcType_', title: '数据库表类型'}
                        ,{field: 'beanType', title: '实体类型(可修改)',edit: 'text'}
                        ,{field: 'nullable', title: '是否可为空'}
                        ,{field: 'length', title: '长度'}
                        ,{field: 'identity', title: '是否自增'}
                        ,{field: 'defaultValue', title: '默认值'}
                        ,{field: 'json', title: '是否json',hide:true}
                        ,{field: 'remarks', title: '备注(可修改)',edit: 'text'}
                        ,{field: 'key', title: '是否主键'}
                    ]]
                    , data:d.allColumns
                });
            }else{
                layer.alert("解析失败->"+data.message)
            }
       })
    }
    layui.config({
        base: '../static/js/layui/extend/'
    }).extend({
        formSelects: 'formSelects-v4'
    });
    var table;
    var formSelects;
    layui.use(['form', 'layedit', 'laydate','formSelects','table'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;
         table = layui.table;
        form.render();
        formSelects = layui.formSelects;
        formSelects.render();

        //自定义验证规则
        form.verify({});

        //监听提交
        form.on('submit(save-data)', function (data) {
            var checkStatus = table.checkStatus('table-data'); //idTest 即为基础参数 id 对应的值
            console.log(checkStatus.data) //获取选中行的数据
            var param = data.field;
             param["allColumnss"]=JSON.stringify(checkStatus.data);
            $.post("../data/simple/build",param,function (data) {
                if(data.code==0){
                    layer.alert("生成成功,具体请查看控制台打印日志")
                }else{
                    layer.alert("生成失败->"+data.message)
                }
            });
            return false
        });

    });
</script>

</body>
</html>